package com.qianbei.view.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

/**
 * 本地缓存的周转箱信息
 * Created by bliu on 2018/3/14.
 */

public class BoxDb {
    private DBUtil dbUtil;
    private Context context;

    public BoxDb(Context context) {
        this.context = context;
        dbUtil = new DBUtil();
    }

    /**
     * 按RFID查询
     *
     * @param rfid      可传null
     * @param boxNumber 可传 null
     * @return 查询不到时返回 null
     */
    public Box getBox(String rfid, String boxNumber) {
        if (TextUtils.isEmpty(rfid) && TextUtils.isEmpty(boxNumber)) {
            return null;
        }

        rfid = TextUtils.isEmpty(rfid) ? "" : rfid;
        boxNumber = TextUtils.isEmpty(boxNumber) ? "" : boxNumber;


        Box box = null;
        SQLiteDatabase db = dbUtil.openDatabase(this.context);

        Cursor cursor;
        if (!TextUtils.isEmpty(rfid)) {
            cursor = db.rawQuery("select * from box where rfid = ?   limit 1", new String[]{rfid});
        } else {
            cursor = db.rawQuery("select * from box where boxNum = ?  limit 1", new String[]{boxNumber});
        }
        if (cursor==null && cursor.getCount() < 1){
            return box;
        }
        if (cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String rfidVal = cursor.getString(cursor.getColumnIndex("rfid"));
            String boxNum = cursor.getString(cursor.getColumnIndex("boxNum"));
            int boxType = cursor.getInt(cursor.getColumnIndex("boxType"));

            box = new Box(rfidVal, boxNum, boxType);
        }
        cursor.close();
        db.close();

        return box;

    }


    /**
     * 更新箱子信息
     *
     * @param box
     */
    public void update(Box box) {
        if (TextUtils.isEmpty(box.getBoxNum()) || box.getBoxNum().trim().length() == 0 ||
                box.getBoxType() == 0) {
            return;
        }

        if (TextUtils.isEmpty(box.getRfid())) {
            box.setRfid("");
        }

        SQLiteDatabase db = dbUtil.openDatabase(this.context);

        Cursor cursor;
        if (!TextUtils.isEmpty(box.getRfid())) {
            cursor = db.rawQuery("select count(*) num from box where rfid = ? ", new String[]{box.getRfid()});
        } else {
            cursor = db.rawQuery("select count(*) num from box where  boxNum = ?", new String[]{box.getBoxNum()});
        }
        long count = 0;
        if (cursor.moveToFirst()) {
            count = cursor.getLong(0);

        }
        cursor.close();

        ContentValues values = new ContentValues();
        values.put("rfid", box.getRfid().trim());
        values.put("boxNum", box.getBoxNum().trim());
        values.put("boxType", box.getBoxType());

        if (count == 0) {
            long affected = db.insert("box", null, values);
            Log.d("insertBox", affected + "");
        } else {
            int affected = db.update("box", values, "rfid = ? or boxNum = ?", new String[]{box.getRfid().trim(), box.getBoxNum().trim()});
            Log.d("updateBox", affected + "");
        }


        db.close();
    }


}
